package your_car_status

import org.springframework.dao.DataIntegrityViolationException
import grails.converters.JSON

class RepairShopController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }
    
    def listCustomer(){
        def cust=RepairShop.get(session.data.id).customer
        [Taller:session.data, customerInstanceList:cust]
    }
    
    def listMechanical(){
        def mech=RepairShop.get(session.data.id).mechanicalTechnician
        [Taller:session.data, mechanicalTechnicianInstanceList:mech]
    }
    
    def newCustomer(){
        [Taller:session.data, customerInstance:new Customer()]
    }

    
    def saveCustomer(){
        def customerInstance = new Customer(params)
        if (!customerInstance.save(flush: true)) {
            render(view: "newCustomer", model: [Taller: session.data, customerInstance: customerInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'customer.label', default: 'Customer'), customerInstance.id])
        redirect(action: "newCustomer")
    }
    
    
    def editCustomer(Long id){
        def customerInstance = Customer.get(id)
        if (!customerInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'customer.label', default: 'Customer'), id])
            redirect(action: "listCustomer")
            return
        }

        [customerInstance: customerInstance]
    }
    
    def showCustomer(Long id) {
        def customerInstance = Customer.get(id)
        if (!customerInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'repairShop.label', default: 'Customer'), id])
            redirect(action: "list")
            return
        }

        [customerInstance: customerInstance]
    }
    
    def updateCustomer(Long id, Long version) {
        def customerInstance = Customer.get(id)
        if (!customerInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'customer.label', default: 'Customer'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (customerInstance.version > version) {
                customerInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'customer.label', default: 'Customer')] as Object[],
                          "Another user has updated this Customer while you were editing")
                render(view: "editCustomer", model: [customerInstance: customerInstance])
                return
            }
        }

        customerInstance.properties = params

        if (!customerInstance.save(flush: true)) {
            render(view: "editCustomer", model: [customerInstance: customerInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'customer.label', default: 'Customer'), customerInstance.id])
        redirect(action: "editCustomer", id: customerInstance.id)
    }
    
    def newMechanicalTechnician(){
        [mechanicalTechnicianInstance:new MechanicalTechnician()]
    }
    
    def listMechanicalTechnician(){
        [mechanicalTechnicianInstanceList: MechanicalTechnician.list()]
    }
    
    def saveMechanicalTechnician(){
        def mechanicalTechnicianInstance = new MechanicalTechnician(params)
        if (!mechanicalTechnicianInstance.save(flush: true)) {
            render(view: "newMechanicalTechnician", model: [mechanicalTechnicianInstance: mechanicalTechnicianInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'mechanicalTechnician.label', default: 'MechanicalTechnician'), mechanicalTechnicianInstance.id])
        redirect(action: "newMechanicalTechnician", id: mechanicalTechnicianInstance.id)
    }
    
    def editMechanicalTechnician(Long id){
        def mechanicalTechnicianInstance = MechanicalTechnician.get(id)
        if (!mechanicalTechnicianInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'mechanicalTechnician.label', default: 'MechanicalTechnician'), id])
            redirect(action: "listMechanicalTechnician")
            return
        }

        [mechanicalTechnicianInstance: mechanicalTechnicianInstance]
    }
    
    def showMechanicalTechnician(Long id) {
        def mechanicalTechnicianInstance = MechanicalTechnician.get(id)
        if (!mechanicalTechnicianInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'repairShop.label', default: 'MechanicalTechnicianInstance'), id])
            redirect(action: "list")
            return
        }

        [mechanicalTechnicianInstance: mechanicalTechnicianInstance]
    }
    
    def updateMechanicalTechnician(Long id, Long version) {
        def mechanicalTechnicianInstance = MechanicalTechnician.get(id)
        if (!mechanicalTechnicianInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'mechanicalTechnician.label', default: 'MechanicalTechnician'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (mechanicalTechnicianInstance.version > version) {
                mechanicalTechnicianInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'mechanicalTechnician.label', default: 'MechanicalTechnician')] as Object[],
                          "Another user has updated this MechanicalTechnician while you were editing")
                render(view: "editMechanicalTechnician", model: [mechanicalTechnicianInstance: mechanicalTechnicianInstance])
                return
            }
        }

        mechanicalTechnicianInstance.properties = params

        if (!mechanicalTechnicianInstance.save(flush: true)) {
            render(view: "editMechanicalTechnician", model: [mechanicalTechnicianInstance: mechanicalTechnicianInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'mechanicalTechnician.label', default: 'MechanicalTechnician'), mechanicalTechnicianInstance.id])
        redirect(action: "editMechanicalTechnician", id: mechanicalTechnicianInstance.id)
    }
    
    def newMechanical(){
        [Taller : session.data, mechanicalTechnicianInstance: new MechanicalTechnician()]
    }
    
    def createPeople(){
        def cust=new Customer();
        cust.repairShop=session.data;
        print(session.data.name)
        def mct=new MechanicalTechnician();
        mct.repairShop=session.data;
        [customerInstance: cust, mechanicalTechnicianInstance: mct ]
    }
    
    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [repairShopInstanceList: RepairShop.list(params), repairShopInstanceTotal: RepairShop.count()]
    }

    def create() {
        [repairShopInstance: new RepairShop(params)]
    }


    def saveMechanical() {
        def mechanicalTechnicianInstance = new MechanicalTechnician(params)
        if (!mechanicalTechnicianInstance.save(flush: true)) {
            render(view: "newMechanical", model: [mechanicalTechnicianInstance: mechanicalTechnicianInstance, Taller: session.data])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'mechanicalTechnician.label', default: 'MechanicalTechnician'), mechanicalTechnicianInstance.id])
        redirect(action: "newMechanical")
    }
    
    def save() {
        def repairShopInstance = new RepairShop(params)
        if (!repairShopInstance.save(flush: true)) {
            render(view: "create", model: [repairShopInstance: repairShopInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'repairShop.label', default: 'RepairShop'), repairShopInstance.id])
        redirect(action: "show", id: repairShopInstance.id)
    }

    def show(Long id) {
        def repairShopInstance = RepairShop.get(id)
        if (!repairShopInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'repairShop.label', default: 'RepairShop'), id])
            redirect(action: "list")
            return
        }

        [repairShopInstance: repairShopInstance]
    }
    
 
    def edit(Long id) {
        def repairShopInstance = RepairShop.get(id)
        if (!repairShopInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'repairShop.label', default: 'RepairShop'), id])
            redirect(controller:'LogIn',action: "logout")
            return
        }

        [repairShopInstance: repairShopInstance]
    }

    
    def update(Long id, Long version) {
        def repairShopInstance = RepairShop.get(id)
        if (!repairShopInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'repairShop.label', default: 'RepairShop'), id])
            redirect(controller:'LogIn',action: "logout")
            return
        }

        if (version != null) {
            if (repairShopInstance.version > version) {
                repairShopInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'repairShop.label', default: 'RepairShop')] as Object[],
                          "Another user has updated this RepairShop while you were editing")
                render(view: "edit", model: [repairShopInstance: repairShopInstance])
                return
            }
        }

        repairShopInstance.properties = params

        if (!repairShopInstance.save(flush: true)) {
            render(view: "edit", model: [repairShopInstance: repairShopInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'repairShop.label', default: 'RepairShop'), repairShopInstance.id])
        redirect(action: "edit", id: repairShopInstance.id)
    }

    def delete(Long id) {
        def repairShopInstance = RepairShop.get(id)
        if (!repairShopInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'repairShop.label', default: 'RepairShop'), id])
            redirect(action: "list")
            return
        }

        try {
            repairShopInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'repairShop.label', default: 'RepairShop'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'repairShop.label', default: 'RepairShop'), id])
            redirect(action: "show", id: id)
        }
    }
    def newServicePackage(){
        [servicesInstance:new Services(),Taller : session.data]
    }
    def saveServicesPackage(){
        def converter = params as JSON;
        def s1=new ServicePackage(jsonData:converter.toString(),name: params.PackageName,repairShop:RepairShop.get(session.data.id))
        if(!s1.save(flush:true)){
            render(view: "newServicePackage", model: [servicesInstance:new Services(),Taller : session.data])
            return
        }
        flash.message = message(code: 'default.created.message', args: [message(code: 'services.label', default: 'Services Package'), s1.id])
        redirect(action: "newServicePackage")
    }
    def listServicePackage(){
        def rs= RepairShop.get(session.data.id).servicePackages;
        [servicesInstanceList: rs ,Taller : session.data]        
    }
}
